﻿using System;
using System.Collections.Generic;
using System.Linq;
using FVG.Entities.Properties;
using FVG.Entities.Repositories;

namespace FVG.Entities.ClientInformation.Repositories
{
	public class CustomerRepository : ParentRepository<Customer>, ICustomerRepository
	{
		public CustomerRepository() : base(Settings.Default.DbName, Settings.Default.MongoConnectionString)
		{
		}

		public IQueryable<Customer> GetAll(CustomerStatus status)
		{
			using (var context = GetContext())
			{
				return context.Queryable<Customer>(w => w.Status == status);
			}
		}

		public override void Remove(Customer entity)
		{
			entity.Status = CustomerStatus.Deleted;
			Save(entity);
		}

		public IList<Website> GetCustomerWebsites(Guid customerId)
		{
			using (var context = GetContext())
			{
				return context.Enumerable<Website>(w => w.ParentId == customerId).ToList();
			}
		}

		public IList<Domain> GetCustomerDomains(Guid customerId)
		{
			using (var context = GetContext())
			{
				var results = new List<Domain>();
				var domainsList = (from c in context.Queryable<Website>(c => c.ParentId == customerId)
							   select c.Domains);

				foreach (var domains in domainsList)
				{
					results.AddRange(domains);
				}

				return results;
			}
		}
	}
}